/*
Copyright (c) 2010 Neil Wood

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package envisage.Controls
{
	import envisage.Data.IArray;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.utils.getDefinitionByName;
	
	public class DropDown extends Control
	{
		public function DropDown()
		{
			super();
			
			
			
			RequireLayout();
			
			addEventListener(Event.ADDED_TO_STAGE, OnAddedToStage);
			
			addEventListener(MouseEvent.MOUSE_DOWN, OnButtonSelected);
		}
		
		private function OnAddedToStage(e:Event):void
		{
			stage.addEventListener(MouseEvent.MOUSE_DOWN, OnStageMouseDown, true);
		}
		
		private function OnStageMouseDown(e:MouseEvent):void
		{
			if( this.hitTestPoint(stage.mouseX, stage.mouseY, true) == true
			||  list.hitTestPoint(stage.mouseX, stage.mouseY, true) == true ) {}
			else Dropped = false;
		}
		
		private var selectedDisplayObject:DisplayObject;
		/*
		private var selectedIndex:int = -1;
		public function get SelectedIndex():int { return selectedIndex; }
		public function get SelectedItem():Object { return DataArray.GetItem(SelectedIndex); }
		public function set SelectedIndex(value:int):void 
		{
			if( selectedIndex != value )
			{
				selectedIndex = value;
				RecreateSelectedItem();
				RequireLayout();
			}
		}*/
		
		private var itemUnselected:Object = null;
		public function set Unselected(value:Object):void
		{
			itemUnselected = value;
			RecreateSelectedItem();
		}
		
		
		public function GetSelectedItem():Object
		{
			return list.GetSelectedItem();
		}
		
		public function SelectItem(item:Object):void
		{
			
			list.SetItemSelected(item, true);
		}
		
		public var ModifyingSelectedItem:Boolean = false;
		
		private function RecreateSelectedItem():void
		{
			if( list == null  )return;
			var item:Object = list.GetSelectedItem();
			
			if( item == null ) item = itemUnselected;
			
			if( item == null ) return;
			
			ModifyingSelectedItem = true;
			list.modify(selectedDisplayObject, item, this);
			ModifyingSelectedItem = false;
		}
		
		public function get DataArray():IArray { return list.DataArray; }
		public function set DataArray(value:IArray):void
		{
			list.DataArray = value;
			//list.SetItemSelected(DataArray.GetItem(0), true);
			//SelectedIndex = 0;
		}
		
		private var button:Button;
		private var list:List;
		
		public function set Button(value:Button):void
		{
			button = value;
			addChild(button);
			//button.addEventListener(UIEvent.SELECTED, OnButtonSelected);
			button.SetToggleMode(true);
			RequireLayout();
		}
		
		public function set List(value:List):void
		{
			list = value;
			list.Owner =this;
			list.visible = false;
			list.MultipleSelection(false);
			Desktop.overlay.addChild(list);
			
			list.addEventListener(EnvisageEvent.SELECTED, OnListItemSelected);
			
			var c:Class = Class(getDefinitionByName(list.itemClassName));
			
			selectedDisplayObject = new c();
			addChild(selectedDisplayObject);
			
			RecreateSelectedItem();
			
			RequireLayout();
		}
		
		private function OnListItemSelected(e:EnvisageEvent):void
		{
			Dropped = false;
			//dispatchEvent(e.clone());
			dispatchEvent(new EnvisageEvent(EnvisageEvent.UPDATED, e.data, this, null));
			RecreateSelectedItem();
		}
		
		private var buttonPos:Point = new Point();
		public function set ButtonPos(value:Point):void { buttonPos = value; RecreateSelectedItem(); RequireLayout(); }
		public function get ButtonPos():Point { return new Point(buttonPos.x, buttonPos.y); }
		
		private var dropped:Boolean = false;
		public function get Dropped():Boolean { return dropped; }
		public function set Dropped(value:Boolean):void { if( dropped != value ) { dropped = value; RequireLayout(); } }
		
		private function OnButtonSelected(e:Event):void
		{
			Dropped = !dropped//button.GetSelected();
			RequireLayout();
		}
		
		protected override function Layout():void
		{
			if( list != null )
			{
				var p:Point = localToGlobal(new Point(0,0));
				list.x = p.x;
				list.width = buttonPos.x;
				list.height = list.DataArray.GetNumItems() * list.ItemSize;
				
				if( Dropped )
				{
					if( (p.y + list.height) > stage.stageHeight ) list.y = p.y - list.height;
					else	list.y = p.y +selectedDisplayObject.height;
				} 
			
				if( Dropped ) 
				{
					if( ! list.visible ) list.visible = true;
				}
			
				else
				{
					if( list.visible ) list.visible = false;
				}
			}
			
			if( button != null )
			{
				if( Dropped != button.GetSelected() ) button.Click();
			//	button.SetSelected(dropped);
				button.x = buttonPos.x;
				button.y = buttonPos.y;
			}
		}
			
	}
}
